<template>
  <view class="content1">
    <view class="custom">
      <!-- /static/arrow_left.png -->
      <image
        class="custom_left"
        src="/static/jiantous.png"
        @click="back"
      ></image>
      <view class="custom_text">我的藏品</view>

      <view class="custom_right" @click="is_show_check = true">批量转赠</view>
    </view>
    <!-- <view class="many_check" @click="is_show_check = true">批量转赠</view> -->

    <view class="exhibit" v-if="Collection.length != 0">
      <view class="Collection">
        <!-- <view class="Collection_top">
 	 		<span >我的藏品 ({{lenthts}})</span>
 	 		<image :src="!GongGestate? '/static/Collections.png':'/static/Collection.png'" style="width: 44rpx;height: 44rpx;" @click="GongGe()"></image>
 	 	</view> -->
        <view :class="!GongGestate ? 'Collection_item' : 'CollectionNew_item'">
          <!-- @click="toCollection(item, index)" -->
          <view
            class="Collection_img"
            @click="handleCheck(index)"
            v-for="(item, index) in Collection"
            :key="index"
          >
            <view class="badge" v-if="can_exchange == 1">兑换</view>
            <view class="">
              <image
                :src="item.product_snap.cover"
                :class="
                  !GongGestate ? 'Collection_image' : 'CollectionNew_image'
                "
              ></image>
              <!-- <u-lazy-load :image="item.product_snap.cover" :loading-img="loadingImg" border-radius="24" style="width: 240rpx;height: 240rpx;min-height: 240rpx;" ></u-lazy-load> -->
            </view>
            <view class="Collection_right">
              <text class="Collection_right_text">{{
                item.product_snap.name
              }}</text>
              <view>
                <image
                  src="/static/collect_icon.png"
                  style="width: 44rpx; height: 44rpx"
                  class="item_farstimg"
                ></image>
                <text class="item_center">{{ item.no }}</text>
              </view>
              <view
                style="display: flex; align-items: center; margin-top: 20rpx"
              >
                <image
                  :src="item.product_snap.creator_avatar"
                  style="
                    width: 34rpx;
                    height: 34rpx;
                    margin-right: 10rpx;
                    border-radius: 50rpx;
                  "
                ></image>
                <text style="font-size: 24rpx" class="creator_name"
                  >{{ item.product_snap.creator_name }}
                </text>
              </view>
            </view>

            <image
              v-if="item.check && is_show_check"
              src="/static/checked.png"
              alt=""
              class="check"
            />
            <image
              v-else-if="!item.check && is_show_check"
              src="/static/circular.png"
              alt=""
              class="check"
            />
          </view>
        </view>
      </view>
    </view>

    <view class="fixed" v-if="is_show_check">
      <image
        src="/static/circular.png"
        alt=""
        class="fixed_check"
        v-if="!all_check"
        @click="handleCheckAll"
      />
      <image
        src="/static/checked.png"
        alt=""
        class="fixed_check"
        v-else
        @click="handleCheckAll"
      />
      <!-- v-if="all_check" -->
      <view class="fixed_text" @click="handleCheckAll">全选</view>
      <view class="fixed_num">已选中{{ check_num() }}个</view>
      <!-- <view v-else class="fixed_text" @click="handleCheckAll(0)">取消全选</view> -->

      <view class="fixed_button1 fixed_button" @click="cancleCheck">取消</view>
      <view class="fixed_button2 fixed_button" @click="toManyCollection"
        >转赠</view
      >
    </view>
    <empty :show="Collection.length == 0 && pagestate"></empty>
  </view>
</template>

<script>
import api from "@/api/api.js";
import tip from "@/utils/tip.js";
import moment from "moment";
import { isLogin } from "@/utils/auth.js";
import empty from "@/components/empty/empty";
export default {
  components: {
    empty,
  },
  data() {
    return {
      widHeigh: 0,

      page: 1,
      limit: 10,
      pagestate: false,

      userInfo: null,

      GongGestate: false,
      Collection: [],
      lenthts: 0,
      product_id: null,
      all_check: false,
      is_show_check: false,

      can_exchange: 0,
    };
  },
  onLoad(option) {
    let windowElement = uni.getSystemInfoSync();
    this.widHeigh = windowElement.windowHeight;
    this.product_id = option.product_id;

    this.can_exchange = option.can_exchange;
  },
  onShow() {
    this.all_check = false;
    this.userInfo = uni.getStorageSync("userInfo");
    this.getproducts();
  },
  methods: {
    check_num() {
      let arr = [];
      this.Collection.forEach((x, i) => {
        if (x.check) {
          arr.push(x);
        }
      });
      return arr.length;
    },
    back() {
      uni.navigateBack({
        delta: 1,
      });
    },
    // async toCollection(itme, index) {
    //   uni.navigateTo({
    //     url: `../../pages/collect_record/collect_record?user_product_id=${this.Collection[index].id}`,
    //   });
    // },
    handleCheck(index) {
      if (this.is_show_check) {
        this.Collection[index].check = !this.Collection[index].check;

        this.all_check = this.Collection.every((x) => x.check);
        this.$forceUpdate();
      } else {
        uni.navigateTo({
          url: `../../pages/collect_record/collect_record?user_product_id=${this.Collection[index].id}`,
        });
      }
    },
    handleCheckAll() {
      this.all_check = this.all_check == 1 ? false : true;
      this.Collection.forEach((x) => {
        if (this.all_check == 0) {
          x.check = false;
        } else {
          x.check = true;
        }
      });
      this.$forceUpdate();
    },
    cancleCheck() {
      this.is_show_check = false;
    },
    async toManyCollection() {
      let arr = [];
      this.Collection.forEach((x, i) => {
        if (x.check) {
          arr.push(x);
        }
      });
      if (arr.length == 0) {
        tip.defaults("至少需要选择一个");
        return;
      }

      uni.setStorageSync("ManyCollection", arr);
      uni.navigateTo({
        url: `../../pages/give_away/give_away?is_many=${true}`,
      });
    },
    GongGe() {
      this.GongGestate = !this.GongGestate;
    },
    async getproducts() {
      // let userInfo = uni.getStorageSync('userInfo')
      // console.log(userInfo.id,'uni.getStod')

      let res = await api.products({
        query: {
          user_id: this.userInfo.id,
          page: this.page,
          limit: this.limit,
          product_id: this.product_id,
        },
      });
      uni.stopPullDownRefresh();
      if (res.code == 0) {
        if (this.page == 1) {
          if (res.data.length > 0) {
            this.Collection = res.data;
            this.Collection.forEach((x) => {
              x.product_snap = JSON.parse(x.product_snap);
            });
            this.lenthts =
              this.userInfo.user_product_num + this.userInfo.blind_box_num;
          } else {
            this.pagestate = true;
          }
        } else {
          if (res.data.length > 0) {
            res.data.forEach((x) => {
              x.product_snap = JSON.parse(x.product_snap);
            });
            this.Collection = [...this.Collection, ...res.data];
            // res.data.forEach((el,index) => {
            // 	this.Collection.push(res.data[index])
            // })
          }
        }

        this.Collection.forEach((x) => {
          x.check = false;
        });
        this.$forceUpdate();
      }
    },

    // onReachBottom() {
    //   this.page++;
    //   this.getproducts();
    // },
    // // 页面下拉刷新
    // onPullDownRefresh() {
    //   this.page = 1;
    //   this.getproducts();
    // },
  },
};
</script>

<style lang="scss" scoped>
@import "../../style/yuanchuang.scss";
.content {
  width: 690rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  padding: 0 30rpx 0 30rpx;
  background: $bg-color1;

  // background-image: url(../../static/index_bg.png);
  // background-repeat: no-repeat;
  // background-size: 100% 510rpx;
}
.content1 {
  padding: 0 0 100rpx 0;
}
.custom {
  display: flex;
  align-items: center;
  position: relative;
  width: 750rpx;
  height: 88rpx;
  text-align: center;
  &_left {
    position: absolute;
    // left: 0;
    // width: 88rpx;
    // height: 80rpx;
    left: 20rpx;
    width: 42rpx;
    height: 42rpx;
  }
  &_text {
    margin: 0 auto;
    // width: 136rpx;
    height: 48rpx;
    font-size: 34rpx;
    font-weight: 500;
    color: #ffffff;
    line-height: 48rpx;
  }
  &_right {
    position: absolute;
    right: 30rpx;
    // width: 112rpx;
    height: 40rpx;
    font-size: 28rpx;
    font-weight: 400;
    color: #1fe7e7;
    line-height: 40rpx;
  }
}
.search {
  width: 690rpx;
  height: 88rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  &_logo {
    width: 192rpx;
    height: 54rpx;
  }
  &_icon {
    width: 40rpx;
    height: 40rpx;
  }
}
.top {
  width: 690rpx;
  height: 280rpx;
  background: black;
  border-radius: 24rpx;
  &_image1 {
    width: 690rpx;
    height: 280rpx;
  }
}
.u-tabs_line {
  display: flex;
  position: relative;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  .u-tabs_wrapper {
    width: 70%;
  }
}
.many_check {
  position: absolute;
  top: 10rpx;
  right: 30rpx;
}
.exhibit {
  overflow: hidden;
  &_item {
    width: 690rpx;
    height: 904rpx;
    background: $bg-color1;
    border-radius: 36rpx;
    &_wrapper {
      position: relative;
      width: 690rpx;
      height: 690rpx;
    }
    &_timer {
      z-index: 1;
      position: absolute;
      top: 26rpx;
      left: 26rpx;

      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20rpx;
      // width: 408rpx;
      height: 60rpx;
      background: #000000;
      border-radius: 30rpx;
      // opacity: 0.46;
      background-color: rgba(0, 0, 0, 0.46);
    }
    &_timerIcon {
      width: 32rpx;
      height: 32rpx;
    }
    &_timerText {
      margin-left: 8rpx;
      font-size: 28rpx;
      font-weight: 400;
      color: #f78623;
      line-height: 40rpx;
      // opacity: 0.46;
    }
    &_sale1 {
    }
    &_sale2 {
      margin-left: 12rpx;
    }
    &_goodsImage {
      border-radius: 36rpx;
      width: 690rpx;
      height: 690rpx;
    }
    &_bottom {
      display: flex;
      align-items: center;
      justify-content: space-between;
    }
    &_left {
      margin: 30rpx 0 0 30rpx;
    }
    &_right {
      height: 138rpx;
      margin: 46rpx 30rpx 0 0;
    }
    &_first {
      width: 400rpx;
      height: 44rpx;
      font-size: 32rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 44rpx;

      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    &_second {
      display: flex;
      align-items: center;

      margin: 20rpx 0;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;

      // width: 202rpx;
      height: 34rpx;
      border-radius: 4rpx;
    }
    &_limited {
      // width: 76rpx;
      padding: 0 14rpx;
      height: 34rpx;
      background: linear-gradient(130deg, #f78623 0%, #d5280a 100%);
      border-radius: 4rpx;
      text-align: center;
    }
    &_inStock {
      padding: 0 14rpx;
      // margin-left: 10rpx;
      font-size: 24rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 34rpx;
      background: #262934;
    }
    &_third {
      display: flex;
      align-items: center;

      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_avatar {
      border-radius: 50%;
      margin-right: 8rpx;
      width: 36rpx;
      height: 36rpx;
    }
    &_username {
      font-size: 26rpx;
      font-weight: 500;
      color: #ffffff;
      line-height: 36rpx;
    }
    &_button {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      line-height: 56rpx;
      color: $text-color4;
      background: $button;
      background-repeat: no-repeat;
      background-size: 100%;
    }
    &_buttonGray {
      margin-left: calc(100% - 116rpx);
      width: 116rpx;
      height: 56rpx;
      background: #999999;
      border-radius: 28rpx;
      text-align: center;

      font-size: 28rpx;
      font-weight: 400;
      color: #ffffff;
      line-height: 56rpx;
    }
    &_price {
      margin-top: 34rpx;
      font-size: 40rpx;
      font-family: SFPro-Bold, SFPro;
      font-weight: bold;
      color: #ffffff;
      line-height: 48rpx;
    }
  }
  &_item:not(:first-child) {
    margin-top: 30rpx;
  }
  &_item:last-child {
    margin-bottom: 30rpx;
  }
}
.Collection {
  color: #ffffff;
  font-size: 32rpx;
  margin: 40rpx 30rpx;
  // background-color: $bg-color1;
}
.Collection_top {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20rpx;
}
.CollectionNew_item {
  display: flex;
  justify-content: space-between;
  flex-direction: row;
  align-items: flex-start;
  flex-wrap: wrap;

  /* align-items: flex-start;
		margin:30rpx 0; */
  .Collection_img {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    background-color: #1c1e27;
    border-radius: 24rpx;
    margin: 15rpx 0 15rpx 0;
    width: 48%;
    height: 540rpx;

    background-image: url("/static/backgroundNew.png");
    background-repeat: no-repeat;
    background-position: center;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;

    .CollectionNew_image {
      width: 330rpx;
      height: 330rpx;
      border-radius: 24rpx;
    }

    .Collection_right {
      margin-left: 30rpx;
      padding: 20rpx 0;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      align-items: flex-start;
      // height: 200rpx;
      .Collection_right_text {
        font-size: 28rpx;
        // padding-top: 15rpx;
        overflow: hidden;
        -webkit-line-clamp: 1;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .Collection_creator_name {
        width: 360rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}
.Collection_item {
  display: flex;
  justify-content: space-between;
  flex-direction: column;
  align-items: flex-start;

  .Collection_img {
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;
    // background-color: #1c1e27;
    border-radius: 24rpx;
    margin: 15rpx 0 15rpx 0;
    width: 100%;
    height: 240rpx;

    background-image: url("/static/background.png");
    background-repeat: no-repeat;
    background-position: center;
    /* background-attachment: fixed; */
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    .Collection_image {
      width: 240rpx;
      height: 240rpx;
      border-radius: 24rpx;
      margin-top: 10rpx;
    }

    .Collection_right {
      margin-left: 30rpx;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      height: 200rpx;
      .Collection_wrapper {
        display: flex;
        align-items: center;
      }
      .Collection_right_text {
        font-size: 28rpx;
        // padding-top: 15rpx;
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .Collection_creator_name {
        width: 360rpx;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
  .check {
    position: absolute;
    right: 30rpx;
    bottom: 30rpx;
    width: 40rpx;
    height: 40rpx;
  }
}
.item_center {
  position: relative;
  left: -30rpx;
  font-size: 20rpx;
  padding: 5rpx 30rpx 5rpx 40rpx;
  border-radius: 17rpx;
  color: $text-color4;
  background-image: $gradient1;

  display: inline-block;
  max-width: 340rpx;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.creator_name {
  width: 320rpx;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}
.item_farstimg {
  z-index: 2;
  position: relative;
  // top: 14rpx;
  top: 4rpx;
  left: -2rpx;
}
.fixed {
  padding: 0 30rpx;
  position: fixed;
  z-index: 999;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  width: 750rpx;
  height: 110rpx;
  background: #292b2b;
  &_check {
    width: 40rpx;
    height: 40rpx;
  }
  &_text {
    margin-left: 16rpx;
    // width: 72rpx;
    height: 50rpx;
    font-size: 36rpx;
    font-weight: 400;
    color: #ffffff;
    line-height: 50rpx;
  }
  &_num {
    position: absolute;
    top: 36rpx;
    left: 160rpx;
    color: #ffffff;
    line-height: 50rpx;
  }
  &_button1 {
    margin-left: 180rpx;
    width: 184rpx;
    height: 80rpx;
    // border-radius: 36rpx;
    // border: 2rpx solid #cccccc;
    text-align: center;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 80rpx;

    color: #ffffff;
    background: url("/static/pl_cancel.png");
    background-repeat: no-repeat;
    background-size: 100%;
  }
  &_button2 {
    margin-left: 24rpx;
    // margin-left: 368rpx;
    width: 184rpx;
    height: 80rpx;
    // border-radius: 36rpx;
    text-align: center;
    font-size: 32rpx;
    font-weight: 400;
    line-height: 80rpx;

    color: #ffffff;
    background: url("/static/pl_confirm.png");
    background-repeat: no-repeat;
    background-size: 100%;
  }
}
.badge {
  width: 88rpx;
  height: 46rpx;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 9;
  background: url("../../static/exchange_badge.png");
  background-repeat: no-repeat;
  background-size: 100%;
  text-align: center;
  font-size: 24rpx;
  font-weight: 400;
  color: #ffffff;
  line-height: 46rpx;
}
</style>
